package com.croot.infra.${projectFlag}.rest.service;

import com.croot.infra.common.annotation.DevopsLog;
import com.croot.infra.common.enums.OperationTypeEnum;
import com.croot.infra.common.protocol.PageResult;
import com.croot.infra.common.util.JacksonUtil;
import com.croot.infra.common.util.RequestUtil;
import com.croot.infra.${projectFlag}.adapter.rdb.consts.ErrorCode;
import com.croot.infra.${projectFlag}.api.basic.${entityName}BasicAPI;
import com.croot.infra.${projectFlag}.dto.request.${entityName}IdReqDTO;
import com.croot.infra.${projectFlag}.dto.request.${entityName}ReqDTO;
import com.croot.infra.${projectFlag}.dto.response.${entityName}IdRespDTO;
import com.croot.infra.${projectFlag}.dto.response.${entityName}RespDTO;
import com.croot.infra.${projectFlag}.exception.${entityName}ServiceException;
import com.croot.infra.${projectFlag}.rest.api.${entityName}RestAPI;
import com.croot.infra.${projectFlag}.rest.dto.request.${entityName}RestReqDTO;
import com.croot.infra.${projectFlag}.rest.dto.request.${entityName}IdRestReqDTO;
import com.croot.infra.${projectFlag}.rest.dto.response.${entityName}IdRestRespDTO;
import com.croot.infra.${projectFlag}.rest.dto.response.${entityName}RestRespDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RefreshScope
public class ${entityName}RestService implements ${entityName}RestAPI {

    @Autowired
    private ${entityName}BasicAPI ${entityParamName}BasicAPI;

    @Override
    public List<${entityName}RestRespDTO> list${entityName}(${entityName}RestReqDTO ${entityParamName}RestReqDTO) {

        List<${entityName}RespDTO> ${entityParamName}RespDTOList = qry${entityName}List(${entityParamName}RestReqDTO);

        if (judge${entityName}RespDTOListIsEmpty(${entityParamName}RespDTOList)) {
            return buildResultOf${entityName}CountIsZero();
        }

        return get${entityName}RestRespDTOList(${entityParamName}RespDTOList);
    }

    @DevopsLog(title = "${entityCN}新增", operationType = OperationTypeEnum.INSERT)
    @Override
    public ${entityName}IdRestRespDTO add${entityName}(${entityName}RestReqDTO ${entityParamName}RestReqDTO) {

        ${entityName}ReqDTO ${entityParamName}ReqDTO = build${entityName}ReqDTO(${entityParamName}RestReqDTO);
        ${entityName}IdRespDTO ${entityParamName}IdRespDTO = ${entityParamName}BasicAPI.add(${entityParamName}ReqDTO);

        ${entityName}IdRestRespDTO ${entityParamName}IdRestRespDTO = new ${entityName}IdRestRespDTO();
        ${entityParamName}IdRestRespDTO.setId(${entityParamName}IdRespDTO.getId());

        RequestUtil.setRequestValue(null, JacksonUtil.toJSON(${entityParamName}RestReqDTO), ${entityParamName}IdRestRespDTO.getId());
        return ${entityParamName}IdRestRespDTO;
    }

    @DevopsLog(title = "${entityCN}删除", operationType = OperationTypeEnum.DELETE)
    @Override
    public void delete${entityName}(${entityName}IdRestReqDTO ${entityParamName}IdRestReqDTO) {
        ${entityName}IdReqDTO ${entityParamName}IdReqDTO = new ${entityName}IdReqDTO();
        ${entityParamName}IdReqDTO.setId(${entityParamName}IdRestReqDTO.getId());

        ${entityName}RespDTO ${entityParamName}RespDTO = getById(${entityParamName}IdReqDTO.getId());

        ${entityParamName}BasicAPI.delete(${entityParamName}IdReqDTO);

        RequestUtil.setRequestValue(JacksonUtil.toJSON(${entityParamName}RespDTO), JacksonUtil.toJSON(${entityParamName}IdRestReqDTO.getId()), ${entityParamName}IdRestReqDTO.getId());
    }

    @DevopsLog(title = "${entityCN}修改", operationType = OperationTypeEnum.UPDATE)
    @Override
    public void update${entityName}(${entityName}RestReqDTO ${entityParamName}RestReqDTO) {
        ${entityName}ReqDTO ${entityParamName}ReqDTO = new ${entityName}ReqDTO();
        BeanUtils.copyProperties(${entityParamName}RestReqDTO, ${entityParamName}ReqDTO);

        ${entityName}RespDTO ${entityParamName}RespDTO = getById(${entityParamName}RestReqDTO.getId());

        ${entityParamName}BasicAPI.update(${entityParamName}ReqDTO);

        RequestUtil.setRequestValue(JacksonUtil.toJSON(${entityParamName}RespDTO), JacksonUtil.toJSON(${entityParamName}RestReqDTO), ${entityParamName}RestReqDTO.getId());
    }

    @Override
    public PageResult<${entityName}RestRespDTO> listPage(${entityName}RestReqDTO ${entityParamName}RestReqDTO) {
        PageResult<${entityName}RestRespDTO> result = new PageResult<>();
        ${entityName}ReqDTO ${entityParamName}ReqDTO = build${entityName}ReqDTO4List(${entityParamName}RestReqDTO);

        List<${entityName}RespDTO> ${entityParamName}RespList = ${entityParamName}BasicAPI.listPage(${entityParamName}ReqDTO);

        Long count = ${entityParamName}BasicAPI.count(${entityParamName}ReqDTO);

        List<${entityName}RestRespDTO> ${entityParamName}RestRespList = get${entityName}RestRespDTOList(${entityParamName}RespList);
        result.setRows(${entityParamName}RestRespList);
        result.setTotal(count);
        return result;
    }

    @Override
    public ${entityName}RestRespDTO getDetail(${entityName}IdRestReqDTO ${entityParamName}IdRestReqDTO) {
        ${entityName}RespDTO ${entityParamName}RespDTO = getById(${entityParamName}IdRestReqDTO.getId());
        return get${entityName}RestRespDTO(${entityParamName}RespDTO);
    }

    public ${entityName}RespDTO getById(String id) {
        ${entityName}IdReqDTO ${entityParamName}IdReqDTO = new ${entityName}IdReqDTO();
        ${entityParamName}IdReqDTO.setId(id);
        ${entityName}RespDTO ${entityParamName}RespDTO = ${entityParamName}BasicAPI.getById(${entityParamName}IdReqDTO);

        if (Objects.isNull(${entityParamName}RespDTO)) {
            throw new ${entityName}ServiceException(ErrorCode.ID_${upperTableName}_NOT_FOUNT.getCode(), ErrorCode.ID_${upperTableName}_NOT_FOUNT.getMsg());
        }

        return ${entityParamName}RespDTO;
    }

    private List<${entityName}RespDTO> qry${entityName}List(${entityName}RestReqDTO ${entityParamName}RestReqDTO) {

        ${entityName}ReqDTO ${entityParamName}ReqDTO = build${entityName}ReqDTO4List(${entityParamName}RestReqDTO);

        List<${entityName}RespDTO> ${entityParamName}RespDTOList = ${entityParamName}BasicAPI.list(${entityParamName}ReqDTO);

        return ${entityParamName}RespDTOList;
    }

    private ${entityName}ReqDTO build${entityName}ReqDTO4List(${entityName}RestReqDTO ${entityParamName}RestReqDTO) {

        ${entityName}ReqDTO ${entityParamName}ReqDTO = new ${entityName}ReqDTO();
        BeanUtils.copyProperties(${entityParamName}RestReqDTO, ${entityParamName}ReqDTO);

        return ${entityParamName}ReqDTO;
    }

    private ${entityName}ReqDTO build${entityName}ReqDTO(${entityName}RestReqDTO ${entityParamName}RestReqDTO) {
        ${entityName}ReqDTO ${entityParamName}ReqDTO = new ${entityName}ReqDTO();
        BeanUtils.copyProperties(${entityParamName}RestReqDTO, ${entityParamName}ReqDTO);

        return ${entityParamName}ReqDTO;
    }

    private boolean judge${entityName}RespDTOListIsEmpty( List<${entityName}RespDTO> ${entityParamName}RespDTOList) {
        return CollectionUtils.isEmpty(${entityParamName}RespDTOList);
    }

    private List<${entityName}RestRespDTO> buildResultOf${entityName}CountIsZero() {
        return new ArrayList<>();
    }

    private List<${entityName}RestRespDTO> get${entityName}RestRespDTOList(List<${entityName}RespDTO> ${entityParamName}RespDTOList) {

        List<${entityName}RestRespDTO> ${entityParamName}List = new ArrayList<>();
        ${entityName}RestRespDTO ${entityParamName}RestRespDTO = null;

        for (${entityName}RespDTO ${entityParamName}RespDTO : ${entityParamName}RespDTOList) {
            if (!Objects.isNull(${entityParamName}RespDTO)) {
                ${entityParamName}RestRespDTO = get${entityName}RestRespDTO(${entityParamName}RespDTO);
                ${entityParamName}List.add(${entityParamName}RestRespDTO);
            }
        }

        return ${entityParamName}List;
    }

    private ${entityName}RestRespDTO get${entityName}RestRespDTO(${entityName}RespDTO ${entityParamName}RespDTO) {
        ${entityName}RestRespDTO ${entityParamName}RestRespDTO = new ${entityName}RestRespDTO();
        BeanUtils.copyProperties(${entityParamName}RespDTO, ${entityParamName}RestRespDTO);
        return ${entityParamName}RestRespDTO;
    }
}
